package com.resourcestore.action;

import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; //
//import net.sf.json.JSONArray;
//import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.resourcestore.model.*;


/**
 * 本类是基础action类，封装了一些必要的action操作方法
 * 
 * @author Administrator
 * 
 */
public class BaseAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	protected Logger logger = null;

	protected ActionContext context = ActionContext.getContext();

	public BaseAction() {
		getLogger();
	}

	private String viewpath;

	public String view(String vpath) {
		viewpath = vpath;
		return "view";
	}

	public String redirect(String vpath) {
		viewpath = vpath;
		return "redirect";
	}

	public String chain(String vpath) {
		viewpath = vpath;
		return "chain";
	}

	public String newpath(String vpath) {
		viewpath = vpath;
		return "newpath";
	}

	/**
	 * 根据用户类型跳转页面
	 * 
	 * @param vpath
	 *            希望跳转的页面（学生身份在此页面前增加stu，即形成stu+vpath的路径）
	 * @param type
	 *            从newpath、chain、redirect中选取一个
	 * @return
	 */
	public String gotoPageByUserType(String vpath, String type,String userType) {
		if (userType.equals("stu")) {// 学生
			int area = vpath.indexOf(".jsp");
			if (area != -1)
				viewpath = vpath.substring(0, vpath.indexOf(".jsp")) + "-stu" + ".jsp";
			else
				viewpath = vpath;
		}else{
			viewpath = vpath;
		}
		return type;
	}

	public String error(String errorId) {
		viewpath = "error.jsp?src=" + errorId;
		return "error";
	}

	public void setViewpath(String viewpath) {
		this.viewpath = viewpath;
	}

	public String getViewpath() {
		return viewpath;
	}

	private void getLogger() {
		if (null == logger) {
			logger = Logger.getLogger(getClass().getName());
		}
	}

	public Map getSession() {

		Map map = ActionContext.getContext().getSession();
		if (null == map) {
			logger.error("session为空！用户需要重新登录");
			ActionContext.getContext().setSession(new HashMap());
			map = ActionContext.getContext().getSession();
			try {
				getResponse().sendRedirect("/error.jsp?errorId=9");
			} catch (IOException e) {
				logger.error("页面跳转时出错！出错信息===" + e);
			}
		}
		return map;
	}

	@Override
	public void setSession(Map arg0) {
		ActionContext.getContext().setSession(arg0);
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public void outString(String str) {
		try {
			PrintWriter out = getResponse().getWriter();
			out.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void outXMLString(String xmlStr) {
		getResponse().setContentType("application/xml;charset=UTF-8");
		outString(xmlStr);
	}

	public void outJsonString(String str) {
		getResponse().setContentType("application/xml;charset=UTF-8");
		// outString("{success:true,root:["+str+"]}");
		outString(str);
		logger.debug("json格式==" + str);
	}

	public void outJsonString(String str, boolean bool) {
		getResponse().setContentType("application/xml;charset=UTF-8");
		if (bool == true)
			outString("{success:true,root:[" + str + "]}");
		else
			outString(str);
		logger.debug("json格式==" + str);
	}

	// public void outJson(Object obj, boolean... args) {
	// if (null == obj)
	// outJsonString("");
	// if (args[0] == true)
	// outJsonString(JSONObject.fromObject(obj).toString(), true);
	// else
	// outJsonString(JSONObject.fromObject(obj).toString());
	// }
	//
	// public void outJsonArray(Object arrayObj) {
	// outJsonString(JSONArray.fromObject(arrayObj).toString());
	// }

//	/**
//	 * 从session中获取userId
//	 * 
//	 * @return
//	 */
//	public int getUsername() {
//		Object obj = this.getSession().get("username");
//		if (null == obj) {
//			try {
//				getResponse().sendRedirect("/error.action?errorId=10");
//				logger.error("session为空！用户需要重新登录");
//			} catch (IOException e) {
//				logger.error(e);
//			}
//		}
//		int resultObj = (Integer) obj;
//		return resultObj;
//
//	}

//	public String getUserName() {
//		Object obj = this.getSession().get("userName");
//		return (String) obj;
//	}

	/**
	 * 从session里取出最近一条记录的url
	 * 
	 * @return
	 */
	public String getUrl() {
		List<String> urlList = (List<String>) this.getSession().get("urlList");
		if (null == urlList) {
			return "sessionError";
		} else {
			return urlList.get(urlList.size() - 1);
		}
	}

	public String error() {
		return SUCCESS;
	}
	
	/**
	 * 显示文件类别的函数
	 * @param majorFileType
	 * @return
	 */
	public String showFileCate(int majorFileType) {
		String mFTName = "";
		switch (majorFileType) {
		case 1:
			mFTName = "文档";
			break;
		case 2:
			mFTName = "压缩包";
			break;
		case 3:
			mFTName = "图片";
			break;
		case 4:
			mFTName = "Flash";
			break;
		case 5:
			mFTName = "视频";
			break;
		case 6:
			mFTName = "音频";
			break;
		}
		return mFTName;
	}
	
	/**
	 * 将是否的代码替换为文字
	 * @param majorFileType
	 * @return
	 */
	public String showIsNot(int isNot) {
		String isNotName = "";
		if(isNot==0){
			isNotName = "否";
		}else if(isNot==1){
			isNotName = "是";
		}
		return isNotName;
	}
}
